﻿using System;
using System.Collections.Generic;
using System.Web;

public static class ScriptUtil
{
    private const string NOTIFY_TEMPLATE = "<p><span class=\"ui-icon ui-icon-info\" style=\"float: left; margin-right: .3em;\"></span>{0}</p>";
    private const string COLUMN_TEMPLATE = "<td>{0}</td>";
    
    private const string ALIGN_RIGHT_COLUMN_TEMPLATE = "<td style=\"text-align:right;\">{0}</td>";
    private const string ALIGN_CENTER_COLUMN_TEMPLATE = "<td style=\"text-align:center;\">{0}</td>";
    private const string HIDDEN_COLUMN_TEMPLATE = "<td><input type=\"hidden\" value=\"{0}\" /></td>";
    private const string TEXT_COLUMN_TEMPLATE = "<td><input type=\"text\" value=\"{0}\" style=\"width:{1}px\" /></td>";
    private const string LINK_COLUMN_TEMPLATE = "<td><a href=\"{1}\">{0}</a></td>";
    private const string DELETE_COLUMN_TEMPLATE = "<td><a class=\"delete\" href=\"\"><img alt=\"Delete\" src=\"Images/Deletered-16.png\" /></a></td>";
    private const string ROW_TEMPLATE = "<tr>{0}</tr>";

    private const string COLUMN_LINE_TEMPLATE = "<td style=\"border: 1px solid #C0C0C0\">{0}</td>";
    private const string COLUMN_COLOR_M_TEMPLATE = "<td style=\"background-color:#c6c6c6; font-weight:bold; font-size:medium; color:Red\">{0}</td>";
    private const string COLUMN_COLOR_TEMPLATE = "<td style=\"background-color:#c6c6c6; font-weight:bold\">{0}</td>";
    private const string ROW_YELLOW_TEMPLATE = "<tr style=\"background-color:#ffffcc\">{0}</tr>";

    public const string DATETIME_FORMAT = "dd/MM/yyyy hh:mm";

    public static string GenerateNotifyMessage(string msg)
    {
        return String.Format(NOTIFY_TEMPLATE, msg);
    }

    public static string GenerateHiddenValue(string content)
    {
        return String.Format(HIDDEN_COLUMN_TEMPLATE, content);
    }

    public static string GenerateDeleteColumn()
    {
        return String.Format(DELETE_COLUMN_TEMPLATE);
    }

    public static string GenerateColumn(string content, string url)
    {
        return String.Format(LINK_COLUMN_TEMPLATE, content, url);
    }

    public static string GenerateColumn(string content)
    {
        return String.Format(COLUMN_TEMPLATE, content);
    }

    public static string GenerateLineColumn(string content)
    {
        return String.Format(COLUMN_LINE_TEMPLATE, content);
    }

    public static string GenerateColorMColumn(string content)
    {
        return String.Format(COLUMN_COLOR_M_TEMPLATE, content);
    }

    public static string GenerateColorColumn(string content)
    {
        return String.Format(COLUMN_COLOR_TEMPLATE, content);
    }

    public static string GenerateTextColumn(string content, string width)
    {
        return String.Format(TEXT_COLUMN_TEMPLATE, content, width);
    }

    public static string GenerateRightColumn(string content)
    {
        return String.Format(ALIGN_RIGHT_COLUMN_TEMPLATE, content);
    }

    public static string GenerateCenterColumn(string content)
    {
        return String.Format(ALIGN_CENTER_COLUMN_TEMPLATE, content);
    }

    public static string GenerateRow(string content)
    {
        return String.Format(ROW_TEMPLATE, content);
    }

    public static string GenerateRowYellow(string content)
    {
        return String.Format(ROW_YELLOW_TEMPLATE, content);
    }

    public static string GenerateJSON(params string[] args)
    {
        if (args.Length == 0)
        {
            return String.Empty;
        }

        string result = String.Empty;

        for (int i = 0; i < args.Length; i++)
        {
            result += String.Format("\"{0}\",", args[i].Replace("'", "\\'").Replace("\"", "\\\""));
        }

        return String.Format("[{0}]", result.Substring(0, result.Length - 1));
    }

    //public static List<string[]> ConverToList(string json)
    //{
    //    List<string[]> list = new List<string[]>();

    //    json = json.Substring(1, json.Length - 2);

    //    if (!String.IsNullOrEmpty(json))
    //    {
    //        json = json.Replace("\"", String.Empty).Replace("],[", "|");
    //        json = json.Replace("[", String.Empty).Replace("]", String.Empty);

    //        string[] rows = json.Split('|');

    //        for (int i = 0; i < rows.Length; i++)
    //        {
    //            list.Add(rows[i].Split(','));
    //        }
    //    }

    //    return list;
    //}

    public static List<string[]> ConverToList(string json)
    {
        List<string[]> list = new List<string[]>();

        json = json.Substring(1, json.Length - 2);

        if (!String.IsNullOrEmpty(json))
        {
            json = json.Replace("\"", String.Empty).Replace("]*,[", "|");
            json = json.Replace("[", String.Empty).Replace("]", String.Empty);

            string[] rows = json.Split('|');

            for (int i = 0; i < rows.Length; i++)
            {
                list.Add(rows[i].Split(new string[] { "*," }, StringSplitOptions.RemoveEmptyEntries));
            }
        }

        return list;
    }


    public static DateTime ConvertToDateTime(string date)
    {
        try
        {
            int y = Convert.ToInt32(date.Substring(6, 4));
            return new DateTime((y > 2500 ? y - 543 : y), Convert.ToInt32(date.Substring(3, 2)), Convert.ToInt32(date.Substring(0, 2)));
        }
        catch
        {
            return DateTime.MinValue;
        }
    }

    public static bool CheckDateTimeFormat(string date)
    {
        if (String.IsNullOrEmpty(date) || ConvertToDateTime(date) == DateTime.MinValue)
        {
            return false;
        }

        return true;
    }
}